<?php
// +----------------------------------------------------------------------
// | 青航系统 [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2023-永久 https://www.feifeiyu.club/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 青航-不吃芒果 <1716892803@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\exception\Jump;
use think\facade\View;
use app\admin\validate\Muen_validate;
class Muen extends  Common{
    
    // 数据列表
    public function index(){
        $list = D('app_menu')->order('sort dsc')->select();
    	$lists = tree_menu($list);
        return View('muen/index',['list'=>$lists]);
    }
    
    
    
    // 添加菜单
    public function add() {
    	$list = D('app_menu')->order('sort dsc')->select();
    	$lists = tree_menu($list);
    	
    	if (request()->isPost()) {
    	    $postField = 'name,parentid,m,c,a,data,icon,sort,status';
			$data = request()->only(explode(',',$postField),'post',null); // 获取全部参数
    	    // 加载验证器
    	    $validate = new Muen_validate();
    	    
            if (!$validate->check($data)) {
               Jump::error($validate->getError());
            }
            
            // 插入数据
    		if(D('app_menu')->insert($data)){
    		    Jump::success("添加成功!");
    		}
            else {
                Jump::error('添加错误!!');
            }
    		
    	}
    	sleep(1);
    	return View('muen/menu_add',['list'=>$lists]);
    }
    
    
    
    
    
    
    //  编辑菜单 
   public function edit() {
    	$id = request()->param('id');
        // 	获取ID的值
    	$list = D('app_menu')->order('sort dsc')->select();
        // 	查询id的数据并渲染到模板
    	$find = D('app_menu')->where('id',$id)->find();
    	$lists = tree_menu($list);
    	
    	if (request()->isPost()) {
    	     // 获取全部参数
    	    $postField = 'id,name,parentid,m,c,a,data,icon,sort,status';
			$data = request()->only(explode(',',$postField),'post',null);
			
    	    // 加载验证器
    	    $validate = new Muen_validate();
    	    
            if (!$validate->check($data)) {
               Jump::error($validate->getError());
            }
            
            // 更新数据 数据中含有主键 使用 save方法
    		if(D('app_menu')->save($data)){
    		    Jump::success("更新成功!");
    		}
            else {
                Jump::error('数据未改动!!');
            }
    		
    	}
    	sleep(1);
    	return View('muen/menu_edit',['list'=>$lists,'find'=>$find]);
    }    
    
    
    
    
    
    
    // 删除菜单
    public function del() {
    	if (request()->isPost()) {
    	   // 获取当前网页的参数ID
    		$id = request()->param('id');
    		if(empty($id) ) {
    			Jump::error('ID不存在!');
    		}
    		D('app_menu')->where('id',$id)->delete();
    		Jump::success("删除成功!!");
    	}
    }    
    
    
    
    
    
    
}